#!/bin/bash

LOG_D=log

DISPATCHER_JAR=dispatcher.jar

NSERVERS=3
SERVER_JAR=server.jar

if [ "$1" == "-k" ]; then
    kill $(ps aux | grep -Ewe "($SERVER_JAR|$DISPATCHER_JAR)" | \
	grep -v grep | awk '{ print $2; }')
    exit 0
fi

PATH=$PATH:.
rm -rf $LOG_D
mkdir -p $LOG_D

echo 'Running dispatcher'
{ $DISPATCHER_JAR > $LOG_D/disp.out 2> $LOG_D/disp.err;
    echo "Dispatcher died with $?"; } &

for ((i=1; i<=NSERVERS; i++)); do
    echo "Running server $i"
    $SERVER_JAR $i >$LOG_D/server_${i}.out 2>$LOG_D/server_${i}.err &
done
